package com.daguchuangyi.erp_android.productlist.parametersetting;

import android.support.v4.util.Pair;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;

import com.daguchuangyi.erp_android.R;
import com.daguchuangyi.erp_android.manager.ParameterManager;
import com.daguchuangyi.erp_android.model.product.Parameter;
import com.daguchuangyi.erp_android.productlist.widget.BaseSlideDialog;
import com.daguchuangyi.erp_android.productlist.widget.SectionView;
import com.daguchuangyi.erp_android.util.ErrorHandler;
import com.jude.utils.JUtils;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Created by lijianjian on 2018/3/9.
 */

public class ParamsSettingDialog extends BaseSlideDialog {

    private CompositeSubscription mCompositeSubscription;

    private LinearLayout mDefaultWrapper;
    private LinearLayout mSectionWrapper;
//    private SectionView mDefaultSection;

    @Override
    public int getLayoutRes() {
        mCompositeSubscription = new CompositeSubscription();
        return R.layout.dialog_product_params_layout;
    }

    @Override
    public int getDialogStyle() {
        return R.style.RightSlideDialog;
    }

    @Override
    public void bindView(View v) {
        v.findViewById(R.id.cancel_tv).setOnClickListener(view -> {
            dismiss();
        });
        v.findViewById(R.id.confirm_tv).setOnClickListener(view -> {
            Set<Parameter> parameterSet = new HashSet<>();
            Set<Integer> localParamSet = new HashSet<>();
//            boolean isImageChecked = mDefaultSection.isChecked();
//            if (isImageChecked) {
//                parameterSet.add(mDefaultSection.getParameter());
//            }
            int subCount = mSectionWrapper.getChildCount();
            JUtils.Log("subCount :" + subCount);
            for (int i = 0; i < subCount; i++) {
                View subItem = mSectionWrapper.getChildAt(i);
                if (subItem instanceof SectionView) {
                    JUtils.Log("find one sectionView");
                    SectionView section = (SectionView) subItem;
                    if (section.isChecked()) {
                        JUtils.Log("add new Item:" + section.getParameter().getName());
                        if (section.isLocalParam()) {
                            localParamSet.add(section.getParameter().getId());
                        }
                        parameterSet.add(section.getParameter());
                    }
                }
            }
            ParameterManager.getInstance().setParameterCheck(parameterSet, localParamSet);
            dismiss();
        });
//        mDefaultSection = v.findViewById(R.id.image_section);
        mDefaultWrapper = v.findViewById(R.id.default_param_wrapper);
        mSectionWrapper = v.findViewById(R.id.sections_wrapper);
        Subscription subscription = ParameterManager.getInstance().listParams(true)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<Pair<ParamsSectionWrapper, ParamsSectionWrapper>>() {
                    @Override
                    public void call(Pair<ParamsSectionWrapper, ParamsSectionWrapper> parameterWrapperPair) {
                        Parameter imgParamter = parameterWrapperPair.first.getParameters().get(0);
                        SectionView defaultSectionView = new SectionView(getContext());
                        defaultSectionView.bindParameter(imgParamter);
                        mDefaultWrapper.addView(defaultSectionView);

//                        mDefaultSection.bindParameter(imgParamter);
//                        mDefaultSection.setVisibility(View.VISIBLE);
//                        mDefaultSection.requestLayout();
//                        mDefaultSection.invalidate();

                        ParamsSectionWrapper sectionWrapper = parameterWrapperPair.second;
                        List<Parameter> parameters = sectionWrapper.getParameters();
//                        JUtils.Log("load paramter success");
                        int height = JUtils.dip2px(56);
                        for (int i = parameters.size() - 1; i >= 0; i--) {
                            SectionView sectionView = new SectionView(getContext());
                            sectionView.bindParameter(parameters.get(i));
                            LinearLayout.LayoutParams layoutParams =
                                    new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, height);
                            mSectionWrapper.addView(sectionView, 0, layoutParams);
                        }
//                        parameterWrapper.getParameters();
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        JUtils.Log("load paramter error");
                        throwable.printStackTrace();
                        ErrorHandler.handlerNetworError(throwable, "加载失败，请检查网络状态");
                    }
                });
        mCompositeSubscription.add(subscription);
    }

    @Override
    public void onDestroyView() {
        if (mCompositeSubscription != null) {
            mCompositeSubscription.clear();
        }
        super.onDestroyView();
    }

    @Override
    public int getWidth() {
        return JUtils.dip2px(280);
    }

    @Override
    public int getHeight() {
        return WindowManager.LayoutParams.MATCH_PARENT;
    }

    @Override
    public int getWindowGravity() {
        return Gravity.RIGHT;
    }
}
